import { loginAPI } from '@/api'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || '',
    refresh_token: localStorage.getItem('refresh_token') || ''
  },
  getters: {},
  mutations: {
    CLEAR_ALL (state) {
      state.token = ''
      state.refresh_token = ''
    },
    SET_TOKEN (state, value) {
      state.token = value
      localStorage.setItem('token', value)
    },
    SET_REFRESH_TOKEN (state, value) {
      state.refresh_token = value
      localStorage.setItem('refresh_token', value)
    }
  },
  actions: {
    async loginActions (store, value) {
      const res = await loginAPI(value)
      store.commit('SET_TOKEN', res.data.data.token)
      store.commit('SET_REFRESH_TOKEN', res.data.data.refresh_token)
      return res // return到逻辑页面当中(async函数始终返回的是一个Promise对象, 所以你的结果会被当做Promise对象成功的结果返回)
    }
  },
  modules: {}
})
